<?php include("/includes/header.php") ?>
<?php include("/includes/sidebar.php") ?>
<?php $idfo = base64_decode($_GET[trim(base64_encode("id"), '=')]); ?>
<?php
	$gender = array('m' => 'Male', 'f' => 'Female');
	$sql = "SELECT * FROM front_office WHERE id = '".$idfo."'";
	$query = mysql_query($sql);
	$data = mysql_fetch_array($query);
	$data = validateData($data);
	extract($data);

	// SELECTING TRAVEL AGEN
	$sql_select_agent = "SELECT * FROM agent WHERE id = '".$idagent."'";
	$query_select_agent = mysql_query($sql_select_agent);
	$data_agent = mysql_fetch_array($query_select_agent);

	// SELECTING SERVICE CHARGE
	$sql_select_service_charge = "SELECT * FROM service_charge WHERE idfront_office = '".$idfo."'";
	$query_select_service_charge = mysql_query($sql_select_service_charge);

	// SELECTING TAX SERVICE
	$sql_select_tax_service = "SELECT * FROM setting";
	$query_select_tax_service = mysql_query($sql_select_tax_service);
	$data_tax_service = mysql_fetch_array($query_select_tax_service);
	$tax_service = $data_tax_service["tax"]+$data_tax_service["service"];

	// SELECTING ROOM INFORMATION
	$data_room = getRoom($idroom);

	// GET CURRENT DATE TO COMPARE WITH CHECKOUT
	$curDate = strtotime(date("Y-m-d h:i:s"));

	// CALCULATE THE DATE BETWEEN CHECKOUT AND CHECKIN
	if ($data["is_checkout"] == 1) {
		$diff = abs(strtotime($data[checkout]) - strtotime($checkin));
		$date_use = date("Y-m-d", strtotime($data[checkout]));
		$disabled_attr = "disabled";
		$checkout_date_use = $data[checkout];
	} else {
		$diff = abs($curDate - strtotime($checkin));
		$date_use = date("Y-m-d", $curDate);
		$disabled_attr = "";
		$checkout_date_use = date("Y-m-d h:i:s");
	}
	$days = floor($diff / (60*60*24));
	
	// $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
	// $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

	// printf("%d years, %d months, %d days\n", $years, $months, $days);
	$grand_total = $data_room["harga"] * $days;


?>

<style type="text/css">
	tbody tr:not(:first-child) td {
		border-top: none !important;
		/*border-bottom: none !important;*/
	}
	tbody tr:not(:last-child) td {
		border-bottom: 1px dotted #ccc !important;
		/*border-bottom: none !important;*/
	}
	.is_checkout {
		background-color: #EEE;
	}
</style>

<!-- <div class="header">
	<span>
		<a href="room.php">
			<i class="fa fa-building-o"></i> Data Kamar
		</a>
	</span>
	<span>
		<a href="room-form.php?act=new">
			<i class="fa fa-plus"></i> Tambah Kamar
		</a>
	</span>
</div> -->

<input type="hidden" id="is_checkout" value="<?php echo $data[is_checkout] ?>">
<div class="content row-fluid">
	<div class="span12 table-placer">
		<div class="alert alert-info"><h4>Checkout Form - <?php echo $data_room["nama"]; ?></h4></div>
		<div class="row-fluid index-pos-placer content">
			<?php
				if ($is_paid == 1) {
			?>
			<div class="alert alert-info">
			  <p>
			  	This person has already been checkout.
			  	<br>
			  	<a href="fo-index.php" class="btn esc">Exit (ESC)</a>
			  </p>
			</div>
			<?php
				} else {
			?>
			<?php if ($curDate < strtotime($checkout)): ?>
				<div class="alert alert-info">It's not the time to checkout yet, however you can keep proceed and the price will be renewed.</div>
			<?php endif ?>
			<form id="form_invoice" action="action/act_fo.php" class="" method="POST">
				<div class="row-fluid">
					<fieldset>
						<legend>Invoice</legend>
						<table class="table table-bordered">
							<thead>
								<tr>
									<th class="al_center" width="50%">Description</th>
									<th class="al_center" width="10%">Price</th>
									<th class="al_center" width="10%">Guests</th>
									<th class="al_center" width="10%">Quantity</th>
									<th class="al_center" width="20%">Amount</th>
								</tr>
							</thead>
							<tbody id="invoice">
								<?php 
									if ($data["is_checkout"] == 1) {
										$tanggal_checkout = date("F j, Y", strtotime($data[checkout]));
										$is_checkout = "is_checkout";
									} else {
										$tanggal_checkout = date("F j, Y", $curDate);
										$is_checkout = "";
									}
								?>
								<tr class="<?php echo $is_checkout; ?>">
									<td class="al_left">
										<h5><?php echo $data_room["nama"]; ?></h5>
										<p> <?php echo date(  "F j, Y", strtotime( $checkin ) )." - ".$tanggal_checkout; ?> </p>
									</td>
									<td class="al_right" valign="middle"><input id="room_price" type="text" class="input-small currency al_right" value="<?php echo $data_room["harga"]; ?>" <?php echo $disabled_attr ?>></td>
									<td class="al_center" valign="middle"><?php echo $banyak_orang; ?><input id="guests_room" type="hidden" value="<?php echo $banyak_orang; ?>"></td>
									<td class="al_center" valign="middle"><?php echo $days; ?> night<?php echo ($days>1) ? "s" : "" ; ?><input id="qty_room" type="hidden" value="<?php echo $days; ?>"></td>
									<td id="amount_room" class="al_right currency amount_inv" valign="middle"><?php echo ($data_room["harga"] * $days); ?></td>
								</tr>
								<?php
									if (mysql_num_rows($query) > 0) {
										$total_service_charge = 0;
										$total_service_charge_temp = 0;
										while ($data_service_charge = mysql_fetch_array($query_select_service_charge)) {
											if ($data_service_charge["is_paid"] == 1) {
												$total_service_charge_temp += ($data_service_charge["jumlah"]*$data_service_charge["harga"]);
												$jumlah_service_charge = 0;
												$is_checkout = "is_checkout";
											} else {
												$is_checkout = "";
											}
											$jumlah_service_charge = $data_service_charge[jumlah];

											$total_service_charge = ($data_service_charge["jumlah"]*$data_service_charge["harga"]);
											$grand_total += $total_service_charge;
								?>
								<tr class="service_charge_arr <?php echo $is_checkout; ?>">
									<td class="al_left"><?php echo $data_service_charge["nama"]; ?></td>
									<td class="al_right currency" valign="middle"><?php echo $data_service_charge["harga"]; ?></td>
									<td class="al_center" valign="middle">
										-
										<input type="hidden" class="name_sc" value="<?php echo $data_service_charge[nama]; ?>">
										<input type="hidden" class="qty_sc" value="<?php echo $jumlah_service_charge; ?>">
										<input type="hidden" class="price_sc currency" value="<?php echo $data_service_charge[harga]; ?>">
									</td>
									<td class="al_center" valign="middle"><?php echo $data_service_charge["jumlah"]; ?></td>
									<td class="al_right currency amount_inv" valign="middle"><?php echo $total_service_charge; ?></td>
								</tr>
								<?php
										}
									}
								?>
							</tbody>
							<tfoot>
								<tr>
									<td class="al_right" colspan="4" valign="middle">
										Total:
									</td>
									<td id="total_temp" class="al_right currency" valign="middle" ><?php echo $grand_total; ?></td>
								</tr>
								<tr>
									<td class="al_right" colspan="4" valign="middle">
										Tax & Service:
									</td>
									<td id="tax_service" class="al_right" valign="middle" data-tax="<?php echo $tax_service; ?>"><?php echo $tax_service; ?> %</td>
								</tr>
								<tr>
									<td class="al_right" colspan="4" valign="middle">
										<?php if ($data["is_checkout"] != 1) { ?>
											<span class="pull-left">
												<button id="add_new_sc" class="btn f3"><i class="fa fa-plus"></i> Add Service Charge (F3)</button>
												<button id="delete_last_sc" class="btn f4"><i class="fa fa-trash-o"></i> Remove Last Line (F4)</button>
											</span>
										<?php } ?>
										Grand Total:
									</td>
									<?php $grand_total_with_tax = ($grand_total+($grand_total*$tax_service/100)); ?>
									<?php if ($data["is_checkout"] == 1) { ?>
										<td id="grand_total_temp" class="al_right currency" valign="middle"><?php echo $grand_total_with_tax; ?></td>
									<?php } else { ?>
										<td id="grand_total" class="al_right currency" valign="middle"><?php echo $grand_total_with_tax; ?></td>
									<?php } ?>
								</tr>
								<?php if ($data["is_checkout"] == 1) { ?>
									<?php  
										$is_paid = 0;
										$is_paid += ($days * $data_room["harga"]);
										$is_paid += $total_service_charge_temp;
										$is_paid += $is_paid * $tax_service/100;
										$grand_total = $grand_total_with_tax - $is_paid;
									?>
									<tr>
										<td class="al_right" colspan="4" valign="middle">
											Paid:
										</td>
										<td id="total_paid" class="al_right currency" valign="middle" ><?php echo $is_paid; ?></td>
									</tr>
									<tr>
										<td class="al_right" colspan="4" valign="middle">
											<?php if ($data["is_checkout"] == 1) { ?>
												<span class="pull-left">
													<button id="add_new_sc" class="btn f3"><i class="fa fa-plus"></i> Add Service Charge (F3)</button>
													<button id="delete_last_sc" class="btn f4"><i class="fa fa-trash-o"></i> Remove Last Line (F4)</button>
												</span>
											<?php } ?>
											Remaining Payment:
										</td>
										<td id="grand_total" class="al_right currency" valign="middle" ><?php echo $grand_total; ?></td>
									</tr>
								<?php } ?>
							</tfoot>
						</table>
					</fieldset>
				</div>
				<div class="row-fluid">
					<div class="accordion" id="accordion2">
					  <div class="accordion-group">
					    <div class="accordion-heading">
					      <span class="accordion-toggle f9" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
					        Visitor's Information (F9)
					      </span>
					    </div>
					    <div id="collapseOne" class="accordion-body collapse">
					      <div class="accordion-inner">
						      <div class="span6">
								<fieldset>
									<legend>Personal Information</legend>
									<input type="hidden" value="checkout" name="act">
									<input type="hidden" value="<?php echo $id; ?>" name="id">
									<dl>
										<dt>Name</dt>
										<dd><?php echo $nama; ?></dd>
										<dt>Occupation</dt>
										<dd><?php echo $pekerjaan; ?></dd>
										<dt>Place / Date of Birth</dt>
										<dd><?php echo $tempat_lahir; ?> / <?php echo date(  "F j, Y", strtotime( $tanggal_lahir ) ); ?></dd>
										<dt>Gender</dt>
										<dd><?php echo $gender[$jenis_kelamin]; ?></dd>
										<dt>Email Address</dt>
										<dd><?php echo $email; ?></dd>
										<dt>Phone Number</dt>
										<dd><?php echo $telepon; ?></dd>
										<dt>Passport Number</dt>
										<dd><?php echo $passport; ?></dd>
										<dt>Address</dt>
										<dd><?php echo $alamat; ?></dd>
									</dl>
								</fieldset>
							</div>
							<div class="span6">
								<fieldset>
									<legend>Visiting Information</legend>
									<dl>
										<dt>Checkin Date</dt>
										<dd><?php echo date(  "F j, Y", strtotime( $checkin ) )." at ".date(  "H:i", strtotime( $checkin ) ); ?></dd>
										<dt>Checkout Date</dt>
										<dd><?php echo date(  "F j, Y", strtotime( $checkout ) )." at ".date(  "H:i", strtotime( $checkout ) ); ?></dd>
										<dt>Arrival Date</dt>
										<dd><?php echo date(  "F j, Y", strtotime( $tanggal_kedatangan ) ); ?></dd>
										<dt>Departure Date</dt>
										<dd><?php echo date(  "F j, Y", strtotime( $tanggal_keberangkatan ) ); ?></dd>
										<dt>Coming From</dt>
										<dd><?php echo $datang_dari; ?></dd>
										<dt>Going To</dt>
										<dd><?php echo $pergi_ke; ?></dd>
										<dt>Purpose of Visit</dt>
										<dd><?php echo $tipe_kunjungan; ?></dd>
										<dt>Agent</dt>
										<dd><?php echo $data_agent["nama"]; ?></dd>
									</dl>
								</fieldset>
							</div>
					      </div>
					    </div>
					  </div>
					</div>
				</div>
				<fieldset>
					<div class="form-actions">
						<button id="submit_checkout" type="submit" class="btn btn-primary f2">Checkout (F2)</button>
						<a id="exit_fo" href="fo-index.php" class="btn esc">Cancel (ESC)</a>
						<button id="print_again" class="btn hide f7" disabled="disabled">Print Again (F8)</button>
						<button id="fast_payment" class="btn btn-info f6 pull-right" <?php echo $disabled_attr; ?>>Fast Payment (F7)</button>
						<button id="print_without_checkout" class="btn btn-info f6 pull-right">Print Without Checking Out (F6)</button>
					</div>
				</fieldset>
			</form>
			<?php
				}
			?>

		</div>
	</div>
</div>

<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-header">
		<a class="close" data-dismiss="modal">&times;</a>
		<h3>Bill Payment</h3>
	</div>
	<div class="modal-body">
		<p>
			<div class="alert alert-info">
			  Total payment is <b id="total_payment"></b>
			</div>
			<div>
				<form id="form_payment">
					<fieldset>
						<div class="control-group">
							<label class="control-label" for="payment_method">Payment Method</label>
							<div class="controls">
								<select id="payment_method" class="input-xlarge">
									<option value="*">--------SELECT PAYMENT METHOD--------</option>
									<option value="1" data-tax="0">Cash</option>
									<option value="2" data-tax="<?php echo $data_tax_service["credit_card"]; ?>">Credit Card</option>
									<option value="3" data-tax="0">Voucher</option>
								</select>
							</div>
						</div>
						<div class="control-group cash_form hide">
							<label class="control-label" for="payment_cash">Cash</label>
							<div class="controls">
								<input id="payment_cash" type="text" class="input-large currency al_right">
							</div>
						</div>
						<div class="control-group cash_form hide">
							<label class="control-label" for="payment_change">Change</label>
							<div class="controls">
								<span id="payment_change" class="currency"></span>
							</div>
						</div>
						<div class="control-group credit_form hide">
							<label class="control-label" for="payment_credit">Payment by Credit Card</label>
							<div class="controls">
								<span id="payment_credit" class="currency"></span>
							</div>
						</div>
						<div class="control-group voucher_form hide">
							<label class="control-label" for="payment_voucher">Voucher Code</label>
							<div class="controls">
								<input id="payment_voucher" type="text" class="input-large">
							</div>
						</div>
					</fieldset>
				</form>
			</div>
		</p>
	</div>
	<div class="modal-footer">
		<a href="#" id="submit_payment" class="btn btn-primary" disabled="disabled">Print Out</a>
		<button id="exit_modal" class="btn" data-dismiss="modal">Close</button>
	</div>
</div>

<div id="myModalFast" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-header">
		<a class="close" data-dismiss="modal">&times;</a>
		<h3>Bill Payment</h3>
	</div>
	<div class="modal-body">
		<p>
			<div class="alert alert-info">
				<?php  
					// $diff = abs(strtotime($checkout) - strtotime($checkin));
					// $amount_day = floor($diff / (60*60*24));
					// echo $amount_day;
					$total_biaya_kamar = ($data_room["harga"] * $days) + $total_service_charge;
					$total_biaya_kamar += $total_biaya_kamar * $tax_service / 100;
				?>
			  Total payment is <b id="total_payment_fast" class="currency"><?php echo $total_biaya_kamar ?></b>
			</div>
			<div>
				<form id="form_payment_fast" class="form-horizontal">
					<fieldset>
						<div class="control-group">
							<label class="control-label" for="inputEmail">Date</label>
						  <div class="controls">
    						<input id="tanggal_checkout_fast" placeholder="Masukkan Tanggal" type="date" data-date="" data-date-format="yyyy-mm-dd" data-date-autoclose="true" data-date-start-view="day" data-date-minviewmode="date" class="datePicker tanggal" value="<?php echo $date_use; ?>">
						  </div>
  					</div>
						<div class="control-group">
							<label class="control-label" for="payment_method">Payment Method</label>
							<div class="controls">
								<select id="payment_method_fast" class="input-xlarge">
									<option value="*">--------SELECT PAYMENT METHOD--------</option>
									<option value="1" data-tax="0">Cash</option>
									<option value="2" data-tax="<?php echo $data_tax_service["credit_card"]; ?>">Credit Card</option>
									<option value="3" data-tax="0">Voucher</option>
								</select>
							</div>
						</div>
						<div class="control-group cash_form hide">
							<label class="control-label" for="payment_cash">Cash</label>
							<div class="controls">
								<input id="payment_cash_fast" type="text" class="input-large currency al_right">
							</div>
						</div>
						<div class="control-group cash_form hide">
							<label class="control-label" for="payment_change">Change</label>
							<div class="controls">
								<span id="payment_change_fast" class="currency"></span>
							</div>
						</div>
						<div class="control-group credit_form hide">
							<label class="control-label" for="payment_credit">Payment by Credit Card</label>
							<div class="controls">
								<span id="payment_credit_fast" class="currency"></span>
							</div>
						</div>
						<div class="control-group voucher_form hide">
							<label class="control-label" for="payment_voucher">Voucher Code</label>
							<div class="controls">
								<input id="payment_voucher_fast" type="text" class="input-large">
							</div>
						</div>
					</fieldset>
				</form>
			</div>
		</p>
	</div>
	<div class="modal-footer">
		<a href="#" id="submit_payment_fast" class="btn btn-primary" disabled="disabled">Print Out</a>
		<button id="exit_modal_fast" class="btn" data-dismiss="modal">Close</button>
	</div>
</div>

<div class="footer">
</div>

<?php include("/includes/footer.php") ?>

<script type="text/javascript">

	function validatePrintOut(status) {
		$('#submit_payment').attr('disabled', 'disabled');
		if (status) {
			$('#submit_payment').removeAttr('disabled');
		}
	}
	function validatePrintOutFast(status) {
		$('#submit_payment_fast').attr('disabled', 'disabled');
		if (status) {
			$('#submit_payment_fast').removeAttr('disabled');
		}
	}

	function toggleEscape(status) {
		if (status) {
			$('#exit_fo').addClass('esc');
			$('#exit_modal').removeClass('esc');
		} else {
			$('#exit_modal').addClass('esc');
			$('#exit_fo').removeClass('esc');
		}
	}

	function toggleEscapeFast(status) {
		if (status) {
			$('#exit_fo').addClass('esc');
			$('#exit_modal_fast').removeClass('esc');
		} else {
			$('#exit_modal_Fast').addClass('esc');
			$('#exit_fo').removeClass('esc');
		}
	}

	function getGrandTotal() {
		var grandTotal = 0;
		$('#invoice .amount_inv').each(function(index, el) {
			grandTotal += parseInt($(el).autoNumeric('get'));
		});
		if ($('#is_checkout').val() == "1") {
			$('#total_temp').autoNumeric('set', grandTotal);
			grandTotal += (grandTotal*parseInt($('#tax_service').data("tax"))/100);
			$('#grand_total_temp').autoNumeric('set', grandTotal);
			remaining = grandTotal - parseInt($('#total_paid').autoNumeric('get'));
			$('#grand_total').autoNumeric('set', remaining);
		} else {
			$('#total_temp').autoNumeric('set', grandTotal);
			grandTotal += (grandTotal*parseInt($('#tax_service').data("tax"))/100);
			$('#grand_total').autoNumeric('set', grandTotal);
		}
	}

	$(document).ready(function() {
		var elPop;
		$('.index-pos').popover();

		$('#room_price').on('keyup', function () {
			/* Act on the event */
			var that = $(this);
			$('#amount_room').autoNumeric('set', (that.autoNumeric('get')*$('#guests_room').val()*$('#qty_room').val()));
			getGrandTotal();
		});

		$('.table-placer').on('click', '.index-pos', function(event) {
			event.preventDefault();
			/* Act on the event */
			// elPop = $(this);
			// $(".index-pos").popover("hide");
		});

		$('#print_again').click(function(event) {
			/* Act on the event */
			event.preventDefault();
			var that = $(this);
			window.open(that.data("href"), "popupWindow", "width=1000,height=600,scrollbars=yes");
		});

		$('#add_new_sc').click(function(event) {
			/* Act on the event */
			event.preventDefault();
			$('#invoice').append("<tr class='added_row service_charge_arr'>"+
				"<td class='al_left'><input type='text' class='input-xlarge name_sc'></td>"+
				"<td class='al_center'><input type='text' class='input-small currency_sc al_right price_sc'></td>"+
				"<td class='al_center'>-</td>"+
				"<td class='al_center'><input type='text' class='input-small qty_sc al_center'></td>"+
				"<td class='al_right amount_sc amount_inv'>0</td>"+
			"</tr>");

			$('.currency_sc').autoNumeric('init', {
				aSign: 'IDR ',
				aPad: false
			});

			$('.amount_sc').autoNumeric('init', {
				aSign: 'IDR ',
				aPad: false
			});

			$(".qty_sc").spinedit({
		    minimum: 1,
		    step: 1,
		    value: 0
			});
			$('#invoice .added_row:last .name_sc').focus();
		});

		$('#invoice').on('keyup', '.price_sc', function(event) {
			/* Act on the event */
			var that = $(this);
			var parent = that.closest('tr');
			var amount = parent.find('.qty_sc').val() * that.autoNumeric('get');
			parent.find('.amount_sc').autoNumeric('set', (amount));
			getGrandTotal();
		});

		$('#invoice').on('valueChanged', '.qty_sc', function(event) {
			// event.preventDefault();
			/* Act on the event */
			var that = $(this);
			var parent = that.closest('tr');
			var amount = parent.find('.price_sc').autoNumeric('get') * that.val();
			parent.find('.amount_sc').autoNumeric('set', (amount));
			getGrandTotal();
		});

		$('#spinEdit').on("valueChanged", function (e) {
		});

		$('#delete_last_sc').click(function(event) {
			/* Act on the event */
			event.preventDefault();
			$('#invoice .added_row:last').remove();
			$('#invoice .added_row:last .name_sc').focus();
			getGrandTotal();
		});

		$('#form_invoice').submit(function(event) {
			/* Act on the event */
			event.preventDefault();
			$('#total_payment').html($('#grand_total').html());
			$('#myModal').modal("show");
		});
		$('#myModal').on('shown', function(event) {
			event.preventDefault();
			/* Act on the event */
			toggleEscape(false);
		});
		$('#myModal').on('hidden', function(event) {
			event.preventDefault();
			/* Act on the event */
			toggleEscape(true);
		});

		$('#form_payment').submit(function(event) {
			/* Act on the event */
			event.preventDefault();
			var serviceChargeArr = [];
			$('.added_row').each(function(index, el) {
				var serviceChargeRowArr = {};
				serviceChargeRowArr.nama = $(el).find('.name_sc').val();
				serviceChargeRowArr.jumlah = $(el).find('.qty_sc').val();
				serviceChargeRowArr.harga = $(el).find('.price_sc').autoNumeric('get');
				serviceChargeArr.push(serviceChargeRowArr);
			});
			var dataCheckout = {
				act: "checkout",
				id: '<?php echo $idfo; ?>',
				checkout: '<?php echo $checkout_date_use ?>',
				payment: $('#payment_method').val(),
				real_paid: $('#room_price').autoNumeric('get'),
				sc: serviceChargeArr
			};
			$.ajax({
				url: 'action/act_fo.php',
				type: 'POST',
				dataType: 'json',
				data: dataCheckout,
				cache: false,
			})
			.done(function(data) {
				if (data.status == '1') {
					var queryString = "&payment_id="+$('#payment_method').val();
					switch ($('#payment_method').val()) {
						case '1':
							queryString += "&payment_cash="+$('#payment_cash').autoNumeric('get');
							break
						case '2':
							queryString += "&credit_tax="+$('#payment_method').find(":selected").data("tax");
							break
						case '3':
							queryString += "&voucher_code="+$('#payment_voucher').val();
							break
					}
					$('#myModal').modal("hide");
					window.open("fo_print_out.php?<?php echo $_SERVER["QUERY_STRING"]; ?>"+queryString, "popupWindow", "width=1000,height=600,scrollbars=yes");
					$('#print_again').data("href", "fo_print_out.php?<?php echo $_SERVER["QUERY_STRING"]; ?>"+queryString).removeAttr('disabled').show();
					$('#submit_checkout').attr('disabled', 'disabled').removeClass('f2');
				};
			})
			.fail(function() {
			})
			.always(function() {
			});
		});

		$('#payment_cash').keyup(function(event) {
			// event.preventDefault();
			/* Act on the event */
			var that = $(this);
			var cash = parseInt(that.autoNumeric('get'));
			var amount = parseInt($('#grand_total').autoNumeric('get'));
			$('#payment_change').autoNumeric('set', 0);
			validatePrintOut(false);
			if ((cash - amount) >= 0) {
				$('#payment_change').autoNumeric('set', (cash - amount));
				validatePrintOut(true);
			};
		});

		$('#payment_method').change(function(event) {
			var that = $(this);
			$('[class*=_form]').hide();
			switch(that.val()){
				case '1':
						validatePrintOut(false);
						$('.cash_form').show();
					break
				case '2':
						validatePrintOut(true);
						var amount = parseInt($('#grand_total').autoNumeric('get'));
						amount += amount * parseInt(that.find(":selected").data("tax")) / 100;
						$('#payment_credit').autoNumeric('set', amount);
						$('.credit_form').show();
					break
				case '3':
						validatePrintOut(false);
						$('.voucher_form').show();
					break
			}
		});

		$('#submit_payment').click(function(event) {
			/* Act on the event */
			event.preventDefault();
			$('#form_payment').submit();
		});

		
		// ====================== FAST CHECKOUT
		$('#fast_payment').click(function(event) {
			event.preventDefault();
			$('#myModalFast').modal("show");
		});
		$('#myModalFast').on('shown', function(event) {
			event.preventDefault();
			toggleEscapeFast(false);
		});
		$('#myModalFast').on('hidden', function(event) {
			event.preventDefault();
			toggleEscapeFast(true);
		});
		$('#form_payment_fast').submit(function(event) {
			event.preventDefault();
			var serviceChargeArr = [];
			$('.added_row').each(function(index, el) {
				var serviceChargeRowArr = {};
				serviceChargeRowArr.nama = $(el).find('.name_sc').val();
				serviceChargeRowArr.jumlah = $(el).find('.qty_sc').val();
				serviceChargeRowArr.harga = $(el).find('.price_sc').autoNumeric('get');
				serviceChargeArr.push(serviceChargeRowArr);
			});
			var dataCheckout = {
				act: "checkout_fast",
				id: '<?php echo $idfo; ?>',
				checkout: $('#tanggal_checkout_fast').val(),
				payment: $('#payment_method_fast').val(),
				real_paid: $('#room_price').autoNumeric('get'),
				sc: serviceChargeArr
			};
			$.ajax({
				url: 'action/act_fo.php',
				type: 'POST',
				dataType: 'json',
				data: dataCheckout,
				cache: false,
			})
			.done(function(data) {
				if (data.status == '1') {
					var queryString = "&payment_id="+$('#payment_method_fast').val();
					switch ($('#payment_method_fast').val()) {
						case '1':
							queryString += "&payment_cash="+$('#payment_cash_fast').autoNumeric('get');
							break
						case '2':
							queryString += "&credit_tax="+$('#payment_method_fast').find(":selected").data("tax");
							break
						case '3':
							queryString += "&voucher_code="+$('#payment_voucher_fast').val();
							break
					}
					window.open("fo_print_out.php?<?php echo $_SERVER["QUERY_STRING"]; ?>"+queryString, "popupWindow", "width=1000,height=600,scrollbars=yes");
					$('#print_again').data("href", "fo_print_out.php?<?php echo $_SERVER["QUERY_STRING"]; ?>"+queryString).removeAttr('disabled').show();
					$('#submit_checkout_fast').attr('disabled', 'disabled').removeClass('f2');
					$('#myModalFast').modal("hide");
				};
			})
			.fail(function() {
			})
			.always(function() {
			});
		});

		$('#payment_cash_fast').keyup(function(event) {
			var that = $(this);
			var cash = parseInt(that.autoNumeric('get'));
			var amount = parseInt($('#total_payment_fast').autoNumeric('get'));
			$('#payment_change_fast').autoNumeric('set', 0);
			validatePrintOutFast(false);
			if ((cash - amount) >= 0) {
				$('#payment_change_fast').autoNumeric('set', (cash - amount));
				validatePrintOutFast(true);
			};
		});

		$('#payment_method_fast').change(function(event) {
			var that = $(this);
			$('[class*=_form]').hide();
			switch(that.val()){
				case '1':
						validatePrintOutFast(false);
						$('.cash_form').show();
					break
				case '2':
						validatePrintOutFast(true);
						var amount = parseInt($('#total_payment_fast').autoNumeric('get'));
						amount += amount * parseInt(that.find(":selected").data("tax")) / 100;
						$('#payment_credit_fast').autoNumeric('set', amount);
						$('.credit_form').show();
					break
				case '3':
						validatePrintOutFast(false);
						$('.voucher_form').show();
					break
			}
		});

		$('#submit_payment_fast').click(function(event) {
			/* Act on the event */
			event.preventDefault();
			$('#form_payment_fast').submit();
		});

		$('#tanggal_checkout_fast').change(function(event) {
			var serviceChargeArr = [];
			$('.service_charge_arr').each(function(index, el) {
				var serviceChargeRowArr = {};
				serviceChargeRowArr.nama = $(el).find('.name_sc').val();
				serviceChargeRowArr.jumlah = $(el).find('.qty_sc').val();
				serviceChargeRowArr.harga = $(el).find('.price_sc').autoNumeric('get');
				serviceChargeArr.push(serviceChargeRowArr);
			});
			var dataCheckout = {
				act: "get_total",
				tanggal: $(this).val(),
				checkin: '<?php echo $checkin ?>',
				id: '<?php echo $idfo; ?>',
				room_price: $('#room_price').autoNumeric('get'),
				sc: serviceChargeArr
			};
			$.ajax({
				url: 'action/act_fo.php',
				type: 'POST',
				dataType: 'json',
				data: dataCheckout,
				cache: false,
			})
			.done(function(data) {
				if (data.status == '1') {
					$('#total_payment_fast').autoNumeric('set', data.amount);
				};
			})
		});

		// $('#payment_cash').on('keypress', 'input', function(event) {
		// 	// event.preventDefault();
		// 	/* Act on the event */
		// });

		$('#print_without_checkout').click(function(event) {
			/* Act on the event */
			event.preventDefault();
			window.open("fo_print_out_wo_checkout.php?<?php echo $_SERVER["QUERY_STRING"]; ?>", "popupWindow", "width=1000,height=600,scrollbars=yes");
		});

		// $('.index-pos').on('hidden.bs.popover', function () {
		// 	elPop.popover("show");
		// })
	});
</script>